package com.fdb.efp.nls.common;

public class ContNoUtil {
    /**
     * 产生合同号校验位
     * @param contNo
     * @return
     */
    public static char getCheckDigitHbs(String contNo) {
        char[] apPure_acct = contNo.toCharArray();
        char digit = ' ';
        int liDigitSum = 0;
        int liNum = 0;
        String lsDigit = "N";
        int i = apPure_acct.length;
        char NewAcc[] = "00000000000000000".toCharArray();

        char wa_table_numbers[][] = {
                "100908070605040302".toCharArray(),
                "051004090308020701".toCharArray(),
                "080502100704010906".toCharArray(),
                "040801050902061003".toCharArray(),
                "020406081001030507".toCharArray(),
                "010203040506070809".toCharArray(),
                "060107020803090410".toCharArray(),
                "030609010407100205".toCharArray(),
                "070310060209050108".toCharArray(),
                "090705030110080604".toCharArray(),
                "100908070605040302".toCharArray(),
                "051004090308020701".toCharArray(),
                "080502100704010906".toCharArray(),
                "040801050902061003".toCharArray(),
                "020406081001030507".toCharArray(),
                "010203040506070809".toCharArray()};

        if (i > 16) {//Account Number length not than 16 
            digit = 'L';
            return digit;
        }

        i--;

        for (int j = 0; j <= i; j++) {//make every byte digital 
            if (apPure_acct[j] - '0' > 9 || apPure_acct[j] - '0' < 0) {
                digit = 'C';
                return digit;
            }
        }

        String.copyValueOf(apPure_acct);
        String tempStr = String.copyValueOf(NewAcc).substring(0, 15 - i) + String.copyValueOf(apPure_acct);
        NewAcc = tempStr.toCharArray();

        for (i = 15; i >= 0; i--) {
            if (NewAcc[i] != '0') {
                lsDigit = "0";
                liNum = NewAcc[i] - '0';
                lsDigit = lsDigit + wa_table_numbers[i][(liNum - 1) * 2];
                lsDigit = lsDigit + wa_table_numbers[i][(liNum - 1) * 2 + 1];
                liDigitSum += Integer.valueOf(lsDigit);
            }
        }
        liDigitSum %= 10;
        digit = (char) ('0' + liDigitSum);
        return digit;
    }
    
}
